// components/record/record.ts
Component({
  data: {
    screenHeight: 0,
    screenWidth: 0,
    sW: 0,
    sH: 0,
    startTime: 0,
    showRecordPanel: false,
    canClean: false,
    canTransform: false,
    iNum: 0,
    hours: '00',
    min: '00',
    sec: '00',
    timer: null,
    status: 0,
    sourcePath: '',
    transformStatus: 0,
    audioToWord: '',
    recorderManager: wx.getRecorderManager()
  },
  methods: {
    doubleNumber(num: number | string) {
      if (num < 10) {
        return '0' + num
      } else {
        return num
      }
    },
    start() {
      this.data.timer = setInterval(() => {
        this.setData({
          iNum: this.data.iNum + 1,
          sec: this.doubleNumber(this.data.iNum % 60),
          min: this.doubleNumber(parseInt((this.data.iNum / 60) % 60)),
          hours: this.doubleNumber(parseInt((this.data.iNum / 3600) % 60))
        })
      }, 1000)
    },
    startRecordHandle(e: any) {
      // 容易触发系统事件
      if (this.data.screenHeight - e.touches[0].pageY < 15) return;
      this.setData({
        startTime: new Date().getTime()
      })
      if (this.data.status) return;
      this.setData({
        status: 1,
        showRecordPanel: true,
      })
      wx.vibrateShort({
        type: 'heavy'
      });
      this.data.recorderManager.start({
        sampleRate: 16000,
        format: 'mp3',
      });
      this.start();
    },
    endRecordHandle() {
      this.data.recorderManager.stop();
    },
    moveHandle(e: any) {
      const {
        pageX,
        pageY
      } = e.changedTouches[0];
      if ((pageX <= this.data.sW && pageY < this.data.sH) || pageX < 100) {
        this.setData({
          canClean: true
        })
      } else {
        this.setData({
          canClean: false
        })
      }
      if ((pageX > this.data.sW && pageY < this.data.sH) || pageX > this.data.screenWidth - 100) {
        this.setData({
          canTransform: false
        })
      } else {
        this.setData({
          canTransform: false
        })
      }
    },
    reset() {
      // innerAudioContext.pause();
      this.setData({
        status: 0,
        sourcePath: '',
        // hours: '00',
        // min: '00',
        // sec: '00',
        iNum: 0,
        canClean: false,
        canTransform: false,
        audioToWord: '',
        transformStatus: 0,
        showRecordPanel: false
      })

      // clearInterval(this.timer)
    },
  },
  ready() {
    const windowInfo = wx.getWindowInfo();
    this.setData({
      screenHeight: windowInfo.screenHeight,
      screenWidth: windowInfo.screenWidth,
      sH: windowInfo.safeArea.height - 120,
      sW: windowInfo.safeArea.width * 0.6
    })

    this.data.recorderManager.onStop(async (res) => {
      // 如果点击速度过快则，手动清除
      if (new Date().getTime() - this.data.startTime < 2000) {
        wx.showToast({
          title: '说话时间太短',
          icon: 'error'
        })
        this.reset();
        return;
      }
      this.setData({
        sourcePath: res.tempFilePath
      })

      if (this.data.canClean) { // 取消
        this.setData({
          showRecordPanel: false
        })
        this.reset();
        return;
      } else if (this.data.canTransform) { // 转文字
        if (this.data.iNum < 1) {
          wx.showToast({
            title: '说话时间太短',
            icon: 'error'
          })
          this.reset()
          return;
        }
        // 语音识别
        // this.setData({
        //     transformStatus:1
        // })
        // const audioToWord = await transformToWord();
        // if (audioToWord) {
        //     this.audioToWord = ''
        //     this.transformStatus = 2;
        // }
        return;
      } else { // 发送
        if (this.data.iNum < 1) {
          wx.showToast({
            title: '说话时间太短',
            icon: 'error'
          })
          this.reset()
        } else {
          this.reset()
          const tempFile = {
            progress: 0,
            path: this.data.sourcePath,
            play: false,
            type: 'voice'
          }
          console.log(tempFile)
        }
      }
    });
  }
})